const pageInfo = {
    page: 1,
    limit: 6,
    pages: 0
}
const http = require("../../utils/http.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        recordsList: [],
        likeList: [],
        pageInfo: {...pageInfo},
        baseImage: http.baseUrl,
        activeName: "records"
    },
    tabChange(e) {
        const type = e.detail.name;
        this.setData({
            recordsList: [],
            likeList: [],
            pageInfo: {...pageInfo}
        })
        if (type === 'records') {
            this.getRecordsList();
        } else {
            this.getLikeList()
        }
    },
    getRecordsList() {
        http.get('wx/vip/record/page', this.data.pageInfo).then(data => {
            this.setData({
                recordsList: [...this.data.recordsList, ...data.records],
                "pageInfo.pages": data.pages
            })
        })
    },
    bookDetail(e) {
        const status = e.currentTarget.dataset.status;
        if (status == 1) {
            const bookId = e.currentTarget.dataset.bookId;
            wx.navigateTo({url: "/pages/book/detail/detail?bookId=" + bookId})
        } else {
            wx.showToast({title: "该书已下架", icon: "error"})
        }

    },
    scrollToBottomRecord() {
        if (this.data.pageInfo.page < this.data.pageInfo.pages) {
            this.setData({
                "pageInfo.page": this.data.pageInfo.page + 1
            })
            this.getRecordsList()
        } else {
            wx.showToast({title: "没有更多图书！"})
        }
    },
    deleteRecord(e) {
        const id = e.currentTarget.dataset.id
        wx.showModal({
            title: "提示",
            content: "确定删除记录吗?",
            showCancel: true,
            success: res => {
                if (res.confirm) {
                    http.delete('wx/vip/record/delete/' + id).then(() => {
                        wx.showToast({title: "删除成功！", icon: "success"})
                        this.setData({
                            recordsList: [],
                            pageInfo: {...pageInfo}
                        })
                        this.getRecordsList()
                    })
                }
            }
        })
    },
    deleteLike(e) {
        const bookId = e.currentTarget.dataset.bookId
        wx.showModal({
            title: "提示",
            content: "确定移除收藏吗?",
            showCancel: true,
            success: res => {
                if (res.confirm) {
                    http.post('wx/vip/like/save', {bookId}).then(() => {
                        wx.showToast({title: "操作成功！", icon: "success"})
                        this.setData({
                            likeList: [],
                            pageInfo: {...pageInfo}
                        })
                        this.getLikeList()
                    })
                }
            }
        })
    },
    scrollToBottomLike() {
        if (this.data.pageInfo.page < this.data.pageInfo.pages) {
            this.setData({
                "pageInfo.page": this.data.pageInfo.page + 1
            })
            this.getLikeList()
        } else {
            wx.showToast({title: "没有更多图书！"})
        }
    },
    getLikeList() {
        http.get('wx/vip/like/page', this.data.pageInfo).then(data => {
            this.setData({
                likeList: [...this.data.likeList, ...data.records],
                "pageInfo.pages": data.pages
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.name) {
            this.setData({
                activeName: options.name
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getRecordsList();
        this.getLikeList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
